package com.gcb.controller;

import com.gcb.constant.MessageConstant;
import com.gcb.constant.RedisMessageConstant;
import com.gcb.entity.Result;
import com.gcb.util.SmsUtils;
import com.gcb.util.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

/**
 * @Author 35235
 * @Date 2021/8/12 19:58
 * @Version 1.0
 */
@RestController
@RequestMapping("/validateCode")
public class ValidateCodeController {
    @Autowired
    private JedisPool jedisPool;
    /**
     * @Author: 35235
     * @Description:用户在线体检预约发送验证码
     * @Date: 2021/8/12 19:59
     * @param: telephone
     * @return:
     */
    @RequestMapping("/send4Order.do")
    public Result send4Order(String telephone) {
        System.out.println("进入验证码的controller");
        //随机生成4位数字验证码
        Integer validateCode = ValidateCodeUtils.generateValidateCode(4);
        //给用户发送验证码
        try {
            SmsUtils.sendShortMessage(SmsUtils.VALIDATE_CODE, telephone, validateCode.toString());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
        //将验证码保存到redis（5分钟）
        jedisPool.getResource().setex(telephone + RedisMessageConstant.SENDTYPE_ORDER, 300, validateCode.toString());
        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }
}
